1_WORKING_EXAMPLE
=================
Files
  update_pico_Ver3.bas        The uploader program (web server + updater). Version string inside: Ver3.
  upload.html                 The page it serves. Heading reads "page Ver3". Must be on the Pico under exactly this name.
  test_program/update_pico_Ver4.bas
                              The same program, padded with comments to 100 KB and reporting "Ver4". Use it to try big installs:
                              Ver3 <-> Ver4 can be installed over each other in both directions.

Before you start
  - The Pico must already be on your WiFi (OPTION WIFI ssid, password; the program waits up to 60 s for an IP address).
  - If a large program does not start after you load it with MMEdit/MMCC, raise "Autosave end delay" in MMCC.inf
    (about line 110): the tool sends RUN too early otherwise. This has nothing to do with this uploader.

First run
  1. Put update_pico_Ver3.bas on the Pico (MMEdit/MMCC) and RUN it. The console prints "Code Good" and
     "Update the Pico Ver3 running at http://<IP>".
  2. Put upload.html on the Pico's flash from the PC, named exactly upload.html. (The page can never overwrite itself on purpose:
     a faulty page could otherwise lock you out.)
  3. Browse to the IP address.

Using the page
  - Web files (.html .js .css .txt, up to 13500 bytes): choose, UPLOAD. Each is written, read back and compared with your copy.
  - Programs (.bas, up to 150000 bytes): choose, UPLOAD. Stored as new.bas, in pieces, checked with an Adler-32 checksum.
    Nothing changes until you press INSTALL: the running program is saved as prev.bas, new.bas is loaded and run (and becomes the
    program that autoruns). The page waits 30 s without sending anything (the firmware can stop a program that gets a request
    while no program is answering), then asks "who is running?" and shows the version. After a good install the page asks the new
    program to delete new.bas / new.enc / new.ok to save flash.
  - ROLLBACK loads prev.bas the same way.
  - Encrypted programs (.enc): see ../3_Encryption/.
  - The page clears its log each time you press UPLOAD, lists the files on the Pico, and shows why an install was stopped.

Try a big install
  Upload test_program/update_pico_Ver4.bas (100 KB, about 40 s), press INSTALL, wait for "running version Ver4", then ROLLBACK.

Rules for every program you install with it
  - It MUST contain this updater (the web server and the routes), or the next update cannot come from the web.
  - Give every release its own  Const PROG_VER$ = "VerN"  so the page can show which one is running. If the version does not change
    the page warns you in red.
  - It should end with the CodeChk sub (Print "Code Good"). INSTALL refuses a program whose last 250 bytes lack the text
    "Code Good": it proves the end of the upload arrived (a cut-short program may still look fine until it is run).
  - File names: 3 to 24 characters, letters, digits, - and _, exactly one dot, extension html js css txt bas enc.

Settings (constants at the top of update_pico_Ver3.bas)
  PAGE_FILE$ "upload.html"   the page. Protected from upload.
  SHOW_DEBUG True            one console line per request; set False when it all works.
  MAX_BYTES 13500            largest web file (limited by the 14 KB RAM buffers).
  MAX_PROG 150000            largest program.
  SWITCH_MS 1500             delay between replying to INSTALL/ROLLBACK and loading the program.
  WDOG_MS 0                  watchdog; 0 = off.
  TAIL_CHECK$ "Code Good"    set to "" to skip the end-of-program check.

How an upload works (why it is built this way)
  Requests are short GET requests (about 1 KB) that fit one network packet, because a request split over two packets is seen as two
  separate requests by this firmware. Each carries a piece of the file: /up?f=NAME&o=OFFSET&l=LENGTH&t=TOTAL[&k=CHECKSUM]&d=DATA.
  Data bytes go as they are for letters, digits and - . _ ~ ! $ ( ) * , / : ; = ? @ , a space as +, everything else as %XX.
  The page sizes each piece to fill one packet and shrinks them by 30% if the Pico answers "incomplete". Retries are safe: a repeated
  piece is just confirmed, a missing one makes the Pico say where to carry on. A program is collected in a 14 KB buffer and appended
  to new.bas / new.enc whenever it fills; new.ok ("bytes,checksum") is written only when the whole file arrived with the right checksum.
  The interrupt only sets a flag; everything is answered in the main loop, one reply and a close per request, "Connection: close".

Memory and flash use (approximate)
  RAM: about 14.4 KB each for the send and upload buffers, 8.8 KB each for the decrypt buffers, a few KB of other buffers.
  Flash: room for the uploaded file plus prev.bas (the backup) plus the running program. A 100 KB program needs about 300 KB free
  during an install (less once cleanup has run).
